package cn.darkhorse.mesadmin.controller.device;


import cn.darkhorse.device.pojo.MesDeviceConsumableInfo;
import cn.darkhorse.device.service.MesDeviceConsumableInfoService;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangtianqi
 * @since 2024-11-06
 */
@RestController
@RequestMapping("/device/mesDeviceConsumableInfo")
@Api(tags = "耗材记录")
public class MesDeviceConsumableInfoController extends BaseController {
    @Autowired
    private MesDeviceConsumableInfoService mesDeviceConsumableInfoService;
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private ISysUserService sysUserService;
    /**
     * 获取耗材类型编码
     * @return
     */
    @ApiOperation(value = "获取编码")
    @GetMapping("/getCode")
    public AjaxResult getCode() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",System.currentTimeMillis());
        return AjaxResult.success(jsonObject);
    }

    /**
     * 获取耗材列表
     * @param mesDeviceConsumableInfo
     * @return
     */
    @ApiOperation(value = "获取耗材列表")
    @GetMapping("/list")
    public TableDataInfo list(MesDeviceConsumableInfo mesDeviceConsumableInfo) {
        startPage();
        List<MesDeviceConsumableInfo> list = mesDeviceConsumableInfoService.list(mesDeviceConsumableInfo);
        for (MesDeviceConsumableInfo consumableInfo : list) {
            if(consumableInfo.getConsumableUnit()!=null){
                consumableInfo.setConsumableUnitName(dictDataService.selectDictLabel("unit_type",consumableInfo.getConsumableUnit()));
            }
            if (consumableInfo.getCreateBy()!=null){
                consumableInfo.setRegistrant(sysUserService.selectUserByUserName(consumableInfo.getCreateBy()).getNickName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 添加或修改耗材
     * @param mesDeviceConsumableInfo
     * @return
     */
    @ApiOperation(value = "添加或修改耗材")
    @PostMapping("/save")
    public AjaxResult save(@RequestBody MesDeviceConsumableInfo mesDeviceConsumableInfo) {
        return AjaxResult.success(mesDeviceConsumableInfoService.saveConsumableInfo(mesDeviceConsumableInfo));
    }

    /**
     * 根据耗材Id获取耗材信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据耗材Id获取耗材信息")
    @GetMapping("/view")
    public AjaxResult getMesDeviceConsumable(Long id) {
        MesDeviceConsumableInfo consumableInfo = mesDeviceConsumableInfoService.view(id);
        if (consumableInfo!=null){
            consumableInfo.setConsumableUnitName(dictDataService.selectDictLabel("unit_type",consumableInfo.getConsumableUnit()));
        }
        return AjaxResult.success(consumableInfo);
    }

    /**
     * 删除耗材
     * @param id
     * @return
     */
    @ApiOperation(value = "删除耗材")
    @GetMapping("/del")
    public AjaxResult delMesDeviceConsumable(Long id) {
        return AjaxResult.success(mesDeviceConsumableInfoService.delete(id));
    }

}

